package com.xianyun.site.support.gatewayimpl;

import com.alibaba.cola.dto.Response;
import com.xianyun.common.redis.service.RedisService;
import com.xianyun.site.support.convertor.SmsConvertor;
import com.xianyun.site.support.domain.model.SmsMessage;
import com.xianyun.site.support.dto.clientobject.SmsMessageCO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/**
 * 短信网关实现
 * @author mac_zyj
 */
@Slf4j
@Component
public class SmsGatewayImpl {

    @Autowired
    private RedisService redisService;

    public Response checkSmsCode(SmsMessageCO message){
        SmsMessage smsMessage = SmsConvertor.toEntity(message);
        String checkKey = smsMessage.getRedisCheckKey();
        Object result=redisService.getCacheObject(checkKey);
        if (null != result){
            String redisCode =result.toString();
            if (smsMessage.codeIsTrue(redisCode)){
                redisService.setCacheObject(checkKey+"_RESULT", message.getSessionId(), 5, TimeUnit.MINUTES);
                log.info("校验成功------Key:{}",checkKey);
                return Response.buildSuccess();
            }

        }
        return Response.buildFailure("BIZ_SUPPORT_SMS_CHECK","校验失败，请确认验证码是否正确");
    }
}
